//
//  FileDownPresonter.m
//  TTKit
//
//  Created by Apple on 2017/11/14.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "FileDownPresonter.h"
#import "TTFileDown.h"
#import "TTFileManager.h"

@implementation FileDownPresonter

-(void)downFile:(NSString*)url
       filePath:(NSString*)filePath{
    __weak typeof(self)weakSelf = self;
    [[TTFileDown sharedManager]downFile:url
                             filePath:filePath
                             progress:^(float progress) {
                                 //TODO
                                 [weakSelf notifyDownProgress:url progress:progress];
                             } sucessed:^{
                                 //TODO
                                 //设置文件不在 iCloud 备份
                                 [[TTFileManager sharedManager] addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:filePath]];
                                 //update ui
                                 [weakSelf notifyDownSuccess:url];
                             } failure:^{
                                 //TODO
                                 [weakSelf notifyDownFail:url];
                             }];
}
#pragma notify down event
-(void)notifyDownSuccess:(NSString*)url{
    for(id<DownListener> ls in self.ListenerArray){
        @try{
           [ls onDownSucessed:url];
            /*if([ls respondsToSelector:@selector(onDownSucessed:)]){
             [ls onDownSucessed:url];
             }*/
        }@catch (NSException *exception) {
        }
    }
}
-(void)notifyDownFail:(NSString*)url{
    for(id<DownListener> ls in self.ListenerArray){
        @try{
            [ls onDownFail:url];
        }@catch (NSException *exception) {
        }
    }
}
-(void)notifyDownProgress:(NSString*)url progress:(float)progress{
    for(id<DownListener> ls in self.ListenerArray){
        @try{
            [ls onDownProgress:url progress:progress];
        }@catch (NSException *exception) {
        }
    }
}
#pragma TaskManager Motheds
-(Boolean)resume:(NSString *)url{
    return [[TTFileDown sharedManager]resume:url];
}
-(void)cancel:(NSString *)url{
    return [[TTFileDown sharedManager]cancel:url];
}
-(Boolean)isExist:(NSString *)url{
    return [[TTFileDown sharedManager]isExist:url];
}
-(int)Count{
    return [[TTFileDown sharedManager]Count];
}

@end
